package com.boe.common.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import lombok.*;

public class JsonUtil {
    private static ObjectMapper getObjectMapper() {
        return new ObjectMapper();
    }

    public static <T> T convertType(Object value, Class<T> type) {
        if (value == null) return null;
        return getObjectMapper().convertValue(value, type);
    }


    @SneakyThrows
    public static <T> T fromGenericJson(String value,
                                        Class<T> type,
                                        Class... genericClasses) {
        if (Strings.isNullOrEmpty(value)) return null;

        var mapper = getObjectMapper();

        var t = mapper.getTypeFactory().constructParametricType(type, genericClasses);
        return mapper.readValue(value, t);

    }
}
